﻿using DevExpress.XtraEditors;
using DevMy.PublicSetUp;
using MDIDemo.PublicClass;
using MDIDemo.PublicSetUp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_BaseDialog : DevExpress.XtraEditors.XtraForm
    {
        public Form_BaseDialog()
        {
            InitializeComponent();
        }
        public int WinType;
        public string MyText;
        public string id;
        protected Class_SetTextEdit class_SetTextEdit;
        protected Class_ComboBoxEdit class_ComboBoxEdit;
        protected Class_SetMemoEdit class_SetMemoEdit;
        protected Class_SQLiteOperator class_SQLiteOperator;
        protected Class_MySqlDataBase class_MySqlDataBase;
        protected Class_RadioGroup class_RadioGroup;
        protected GridC gridC;

        protected void SetComponent()
        {
            class_RadioGroup = new Class_RadioGroup();
            class_SetTextEdit = new Class_SetTextEdit();
            class_ComboBoxEdit = new Class_ComboBoxEdit();
            class_SetMemoEdit = new Class_SetMemoEdit();
            class_SQLiteOperator = new Class_SQLiteOperator();
            gridC = new GridC();
            Class_PublicMethod class_PublicMethod = new Class_PublicMethod();
            Class_DataBaseConDefault class_DataBaseConDefault = new Class_DataBaseConDefault();
            class_DataBaseConDefault = class_PublicMethod.FromXmlToDefaultValueObject<Class_DataBaseConDefault>("DataBaseDefaultValues");

            class_MySqlDataBase = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , class_DataBaseConDefault.dataBaseName
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
        }

        /// <summary>
        /// 展示已有数据
        /// </summary>
        /// <param name="id">主键</param>
        protected virtual void DisplayComponent(string id) { }
        protected virtual void SetComboBoxEdit() { }

        private void SetReadOnly(Control NewControl)
        {
            foreach (Control control in NewControl.Controls)
            {
                #region
                if (control is TextEdit)
                {
                    TextEdit textEdit = control as TextEdit;
                    textEdit.ReadOnly = true;
                }
                if (control is CheckEdit)
                {
                    CheckEdit checkEdit = control as CheckEdit;
                    checkEdit.ReadOnly = true;
                }
                if (control is ComboBoxEdit)
                {
                    ComboBoxEdit comboBoxEdit = control as ComboBoxEdit;
                    comboBoxEdit.ReadOnly = true;
                }
                if (control is RadioGroup)
                {
                    RadioGroup radioGroup = control as RadioGroup;
                    radioGroup.ReadOnly = true;
                }
                if (control is SpinEdit)
                {
                    SpinEdit spinEdit = control as SpinEdit;
                    spinEdit.ReadOnly = true;
                }
                if (control is MemoEdit)
                {
                    MemoEdit memoEdit = control as MemoEdit;
                    memoEdit.ReadOnly = true;
                }
                if (control is LabelControl)
                {
                    LabelControl labelControl = control as LabelControl;
                    if (labelControl.Text.IndexOf("说明") < 0)
                    {
                        labelControl.Appearance.Font = new System.Drawing.Font("Tahoma", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                        labelControl.Appearance.Options.UseFont = true;
                    }
                }
                #endregion

                if (control.HasChildren)
                    SetReadOnly(control);
            }
        }
        private void Form_BaseDialog_Load(object sender, EventArgs e)
        {
            switch (this.WinType)
            {
                case -1:
                    this.Text += string.Format("{0}---查看", MyText);
                    this.simpleButton1.Enabled = false;
                    SetReadOnly(this.panelControl1);
                    break;
                case 0:
                    this.Text += string.Format("{0}---修改", MyText);
                    break;
                case 1:
                    this.Text += string.Format("{0}---增加", MyText);
                    break;
                default:
                    this.Text += string.Format("{0}---查看", MyText);
                    this.simpleButton1.Enabled = false;
                    SetReadOnly(this.panelControl1);
                    break;
            }
            DisplayComponent(id);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
